/*==============================================================================
FILE NAME: Table_1.do
CREATED: 1 July 2025
==============================================================================*/

** Table 1

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/


//Panel A: : ∆ P(Investigation_t)
use "$processed_data/facility_characteristics.dta", clear 
keep RegulatedEntityNo SIC
drop if SIC==""
gen dig2_SIC=substr(SIC,1,2)
destring dig2_SIC, replace
tab dig2_SIC
drop SIC
duplicates drop
gen RN_id=substr(RegulatedEntityNo,3,.)
label var RN_id "same as RN without 'RN'"
destring RN_id, replace
drop RegulatedEntityNo
sort RN_id dig2_SIC
by RN_id: gen num=_n
reshape wide dig2_SIC, i(RN_id) j(num)
gen multiple_SIC=0
replace multiple_SIC=1 if dig2_SIC2~=.
replace dig2_SIC1=100 if multiple_SIC==1
sort RN_id
save "$processed_data/temp.dta", replace

use "$processed_data/Air_Panel_with_referred_clean.dta", clear
sort RN_id
merge m:1 RN_id using "$processed_data/temp.dta"
unique RN_id if _merge!=2 
keep if _merge!=2
unique RN_id if multiple_SIC==1
drop dig2_SIC2-dig2_SIC12
unique RN_id if dig2_SIC1~=.
tab dig2_SIC1

foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
drop if p_air_inv_`h' == .
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
drop if p_air_inv_neg`h' == .
}

egen industry_month=group(dig2_SIC1 t)
egen county_id=group(county)
egen county_month=group(county t)

cap drop b u d se b_ref se_ref u_ref d_ref Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0
gen b_ref = 0
gen se_ref = 0
gen u_ref = 0 
gen d_ref = 0


eststo i1: reghdfe p_air_inv_0 p_air_incident, absorb(RN_year t) cluster(RN_id )
eststo i2: reghdfe p_air_inv_0 p_referred p_air_incident, absorb(RN_year t) cluster(RN_id )
eststo i3: reghdfe p_air_inv_0 p_air_incident p_ee_incident, absorb(RN_year t) cluster(RN_id )
eststo i4: reghdfe p_air_inv_0 p_air_incident p_ee_incident, absorb(RN_year t county_month) cluster(RN_id )
eststo i5: reghdfe p_air_inv_0 p_air_incident p_ee_incident, absorb(RN_year t county_month industry_month) cluster(RN_id )

*xtset addition
xtset RN_id t


eststo i6: reghdfe p_air_inv_0 p_air_incident l(0/12).p_ee_incident, absorb(RN_year t county_month industry_month) cluster(RN_id )
esttab i1 i2 i3 i4 i5 using "$tables/Table_1_Panel_A.tex", f legend se nomtitles stats(N r2 ar2) keep(p_air_incident p_referred p_ee_incident) booktabs replace nonotes star(* 0.10 ** 0.05 *** 0.01)


//Panel B: ∆ P(NOV_t+2)
eststo i1: reghdfe p_air_nov_2 p_air_incident, absorb(RN_year t) cluster(RN_id )
eststo i2: reghdfe p_air_nov_2 p_referred p_air_incident, absorb(RN_year t) cluster(RN_id )
eststo i3: reghdfe p_air_nov_2 p_air_incident p_ee_incident, absorb(RN_year t) cluster(RN_id )
eststo i4: reghdfe p_air_nov_2 p_air_incident p_ee_incident, absorb(RN_year t county_month) cluster(RN_id )
eststo i5: reghdfe p_air_nov_2 p_air_incident p_ee_incident, absorb(RN_year t county_month industry_month) cluster(RN_id )
eststo i6: reghdfe p_air_nov_2 p_air_incident l(0/12).p_ee_incident, absorb(RN_year t county_month industry_month) cluster(RN_id )
esttab i1 i2 i3 i4 i5 using "$tables/Table_1_Panel_B.tex", f legend se nomtitles stats(N r2 ar2) keep(p_air_incident p_referred p_ee_incident) booktabs replace nonotes star(* 0.10 ** 0.05 *** 0.01)

//Panel C: ∆ P(NOE_t+2)
eststo i1: reghdfe p_air_noe_2 p_air_incident, absorb(RN_year t) cluster(RN_id )
eststo i2: reghdfe p_air_noe_2 p_referred p_air_incident, absorb(RN_year t) cluster(RN_id )
eststo i3: reghdfe p_air_noe_2 p_air_incident p_ee_incident, absorb(RN_year t) cluster(RN_id )
eststo i4: reghdfe p_air_noe_2 p_air_incident p_ee_incident, absorb(RN_year t county_month) cluster(RN_id )
eststo i5: reghdfe p_air_noe_2 p_air_incident p_ee_incident, absorb(RN_year t county_month industry_month) cluster(RN_id )
eststo i6: reghdfe p_air_noe_2 p_air_incident l(0/12).p_ee_incident, absorb(RN_year t county_month industry_month) cluster(RN_id )
esttab i1 i2 i3 i4 i5 using "$tables/Table_1_Panel_C.tex", f legend se nomtitles stats(N r2 ar2) keep(p_air_incident p_referred p_ee_incident) booktabs replace nonotes star(* 0.10 ** 0.05 *** 0.01)
